﻿Imports System.Web.UI.WebControls

''' <summary>
''' Provides extension methods to the <see cref="ListControl">ListControl</see> object.
''' </summary>
''' <revisions>
'''   <revision date="11/02/2008" author="Chad Green" version="1.0.0.0">Initial Development</revision>
''' </revisions>
Public Module ListControlExtensions

  ''' <summary>
  ''' Sorts the <see cref="ListControl">ListControl</see> by it's <see cref="ListControl.Text">Text</see> property.
  ''' </summary>
  ''' <param name="value">The <see cref="ListControl">ListControl</see> to be sorted.</param>
  ''' <example>
  '''   <para>The following examples sort the lbxDisplayValue by the listbox items' text value.</para>
  '''   <code language="vb">
  '''     Private Sub SortButton_Click(ByVal sender As Object, ByVal e As System.EventArgs)
  '''       lbxDisplayValue.SortByText
  '''     End Sub
  '''   </code>
  '''   <code language="c#">
  '''     private void SortButton_Click(object sender, System.EventArgs e)
  '''     {
  '''       lbxDisplayValue.SortByText();
  '''     }
  '''   </code>
  ''' </example>
  ''' <revisions>
  '''   <revision date="11/02/2008" author="Chad Green" version="1.0.0.0">Initial Development</revision>
  ''' </revisions>
  <Extension()> Public Sub SortByText(ByVal value As ListControl)

    ' Variable Declarations
    Dim aylDropDownListItems As New ArrayList
    Dim intListItem As Integer
    Dim arrListItem As String()
    Dim strSelectedValue As String = value.SelectedValue
    Dim objListItem As ListItem

    ' Pull out the list items
    For intListItem = 0 To value.Items.Count - 1
      aylDropDownListItems.Add(value.Items(intListItem).Text.ToString & "|" & value.Items(intListItem).Value.ToString)
    Next intListItem

    ' Sort the items
    aylDropDownListItems.Sort()

    ' Repopulate the DropDownList
    value.Items.Clear()
    For intListItem = 0 To aylDropDownListItems.Count - 1
      arrListItem = aylDropDownListItems.Item(intListItem).ToString.Split(Convert.ToChar("|"))
      objListItem = New ListItem(arrListItem(0), arrListItem(1))
      If objListItem.Value = strSelectedValue Then
        objListItem.Selected = True
      End If
      value.Items.Add(objListItem)
    Next intListItem

  End Sub

  ''' <summary>
  ''' Sorts the <see cref="ListControl">ListControl</see> by it's <see cref="ListControl.Text">Text</see> property.
  ''' </summary>
  ''' <param name="value">The <see cref="ListControl">ListControl</see> to be sorted.</param>
  ''' <example>
  '''   <para>The following examples sort the lbxDisplayValue by the listbox items' value value.</para>
  '''   <code language="vb">
  '''     Private Sub SortButton_Click(ByVal sender As Object, ByVal e As System.EventArgs)
  '''       lbxDisplayValue.SortByValue
  '''     End Sub
  '''   </code>
  '''   <code language="c#">
  '''     private void SortButton_Click(object sender, System.EventArgs e)
  '''     {
  '''       lbxDisplayValue.SortByValue();
  '''     }
  '''   </code>
  ''' </example>
  ''' <revisions>
  '''   <revision date="11/02/2008" author="Chad Green" version="1.0.0.0">Initial Development</revision>
  ''' </revisions>
  <Extension()> Public Sub SortByValue(ByVal value As ListControl)

    ' Variable Declarations
    Dim aylDropDownListItems As New ArrayList
    Dim intListItem As Integer
    Dim arrListItem As String()

    ' Pull out the list items
    For intListItem = 0 To value.Items.Count - 1
      aylDropDownListItems.Add(value.Items(intListItem).Value.ToString & "|" & value.Items(intListItem).Text.ToString)
    Next intListItem

    ' Sort the items
    aylDropDownListItems.Sort()

    ' Repopulate the DropDownList
    value.Items.Clear()
    For intListItem = 0 To aylDropDownListItems.Count - 1
      arrListItem = aylDropDownListItems.Item(intListItem).ToString.Split(Convert.ToChar("|"))
      value.Items.Add(New ListItem(arrListItem(1), arrListItem(0)))
    Next intListItem

  End Sub

  ''' <summary>
  ''' Gets a string array representing the values from the checked items in the <see cref="ListControl">ListControl</see>.
  ''' </summary>
  ''' <param name="value">The <see cref="ListControl">ListControl</see> to retrieve from.</param>
  ''' <returns>An array of <see cref="String">String</see> containing the values from checked items within the specified <see cref="ListControl">ListControl</see>.</returns>
  ''' <example>
  '''   <para>The following examples sort the call a method on an entity object specifying the checked items from a CheckBoxList.</para>
  '''   <code language="vb">
  '''     Private Sub ProcessButton_Click(ByVal sender As Object, ByVal e As System.EventArgs)
  '''       ApprovalQueue.Approve(cblItemsToProcess.ToArray)
  '''     End Sub
  '''   </code>
  '''   <code language="c#">
  '''     private void ProcessButton_Click(object sender, System.EventArgs e)
  '''     {
  '''     	ApprovalQueue.Approve(cblItemsToProcess.ToArray);
  '''     }
  '''   </code>
  ''' </example>
  ''' <revisions>
  '''   <revision date="11/02/2008" author="Chad Green" version="1.0.0.0">Initial Development</revision>
  ''' </revisions>
  <Extension()> Public Function ToArray(ByVal value As ListControl) As String()

    ' Variable Declarations
    Dim strReturnValue() As String = Nothing
    Dim aylReturnValue As New ArrayList

    ' Build the results
    For Each liItem As ListItem In value.Items
      If liItem.Selected = True Then
        aylReturnValue.Add(liItem.Value)
      End If
    Next liItem
    If aylReturnValue.Count > 0 Then
      ReDim strReturnValue(aylReturnValue.Count - 1)
      For intItem As Integer = 0 To aylReturnValue.Count - 1
        strReturnValue(intItem) = aylReturnValue.Item(intItem).ToString
      Next intItem
    End If

    ' Return the results
    Return strReturnValue

  End Function

  ''' <summary>
  ''' Adds a new item to the <see cref="ListControl">ListControl</see>.
  ''' </summary>
  ''' <param name="listControl">The list control to manipulate.</param>
  ''' <param name="text">The text value for the new list item.</param>
  ''' <param name="value">The value for the new list item.</param>
  ''' <example>
  '''   <para>The following examples populate a drop down list using the data from a dataset.</para>
  '''   <code language="vb">
  '''     Public Sub PopulateEquipmentTypeList()
  '''       Dim dsEquipmentTypes As DataSet = EquipmentType.GetList()
  '''       If dsEquipmentTypes.HasData Then
  '''         For Each drEquipmentType As DataRow In dsEquipmentTypes.Tables(0).Rows
  '''           ddlEquipmentType.AddItem(drEquipmentType.GetStringValue("Description"), drEquipmentType.GetInt32Value("ID").ToString)
  '''         Next drEquipmentType
  '''       End If
  '''     End Sub
  '''   </code>
  '''   <code language="c#">
  '''     public void PopulateEquipmentTypeList()
  '''     {
  '''     	DataSet dsEquipmentTypes = EquipmentType.GetList();
  '''     	if (dsEquipmentTypes.HasData)
  '''     	{
  '''     		foreach (DataRow drEquipmentType in dsEquipmentTypes.Tables(0).Rows) {
  '''     			ddlEquipmentType.AddItem(drEquipmentType.GetStringValue("Description"), drEquipmentType.GetInt32Value("ID").ToString);
  '''     		}
  '''     	}
  '''     }
  '''   </code>
  ''' </example>
  ''' <revisions>
  '''   <revision date="11/02/2008" author="Chad Green" version="1.00.000">Initial Development</revision>
  ''' </revisions>
  <Extension()> Public Sub AddItem(ByVal listControl As ListControl, _
                                   ByVal text As String, _
                                   ByVal value As String)
    AddItem(listControl, text, value, False)
  End Sub

  ''' <summary>
  ''' Adds a new item to the <see cref="ListControl">ListControl</see>.
  ''' </summary>
  ''' <param name="listControl">The list control to manipulate.</param>
  ''' <param name="text">The text value for the new list item.</param>
  ''' <param name="value">The value for the new list item.</param>
  ''' <param name="selected">If set to <c>true</c> preselect the new list item.</param>
  ''' <remarks>If <paramref name="selected">selected</paramref> equals <c>true</c>, any other list item in the <see cref="ListControl">ListControl</see> will be unselected.</remarks>
  ''' <example>
  '''   <para>The following examples populate a drop down list using the data from a dataset and selecting the specified value.</para>
  '''   <code language="vb">
  '''     Public Sub PopulateEquipmentTypeList(ByVal SelectedValue As Integer)
  '''       Dim dsEquipmentTypes As DataSet = EquipmentType_GetList()
  '''       If dsEquipmentTypes.HasData Then
  '''         For Each drEquipmentType As DataRow In dsEquipmentTypes.Tables(0).Rows
  '''           If SelectedValue = drEquipmentType.GetInt32Value("ID") Then
  '''             ddlEquipmentType.AddItem(drEquipmentType.GetStringValue("Description"), drEquipmentType.GetInt32Value("ID").ToString, True)
  '''           Else
  '''             ddlEquipmentType.AddItem(drEquipmentType.GetStringValue("Description"), drEquipmentType.GetInt32Value("ID").ToString, False)
  '''           End If
  '''         Next drEquipmentType
  '''       End If
  '''     End Sub
  '''   </code>
  '''   <code language="c#">
  '''     public void PopulateEquipmentTypeList(int SelectedValue)
  '''     {
  '''     	DataSet dsEquipmentTypes = EquipmentType_GetList();
  '''     	if (dsEquipmentTypes.HasData)
  '''     	{
  '''     		foreach (DataRow drEquipmentType in dsEquipmentTypes.Tables(0).Rows) {
  '''     			if (SelectedValue == drEquipmentType.GetInt32Value("ID"))
  '''     			{
  '''     ddlEquipmentType.AddItem(drEquipmentType.GetStringValue("Description"), drEquipmentType.GetInt32Value("ID").ToString, true);
  '''     			}
  '''     			else
  '''     			{
  '''     				ddlEquipmentType.AddItem(drEquipmentType.GetStringValue("Description"), drEquipmentType.GetInt32Value("ID").ToString, false);
  '''     			}
  '''     		}
  '''     	}
  '''     }
  '''   </code>
  ''' </example>
  ''' <revisions>
  '''   <revision date="11/02/2008" author="Chad Green" version="1.0.0.0">Initial Development</revision>
  ''' </revisions>
  <Extension()> Public Sub AddItem(ByVal listControl As ListControl, _
                                   ByVal text As String, _
                                   ByVal value As String, _
                                   ByVal selected As Boolean)

    ' Crate the list item
    Dim liNewItem As New ListItem(text, value)

    ' If selected, ensure there are no other selected items in the list control and set the new list item's selected status
    If selected = True Then
      For Each liCurrent As ListItem In listControl.Items
        liCurrent.Selected = False
      Next liCurrent
      liNewItem.Selected = True
    Else
      liNewItem.Selected = False
    End If

    ' Add new item to the control
    listControl.Items.Add(liNewItem)

  End Sub

End Module